﻿// https://leetcode-cn.com/problems/path-sum/
// 112. 路径总和

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) return false;
        return FindSum(root, targetSum - root->val);
    }

    bool FindSum(TreeNode* root, int count) {
        if (!root->left && !root->right && count == 0) return true;
        if (!root->left && !root->right) return false;

        if (root->left) {
            if (FindSum(root->left, count - root->left->val))
                return true;
        }
        if (root->right) {
            if (FindSum(root->right, count - root->right->val))
                return true;
        }
        return false;
    }
};

int main() {
    Solution obj = Solution();
}